package cn.cdyx.ydhl.interceptor;

/**
 * @Author Watermelon
 * @Date 2023-01-30 16:08
 */

import cn.cdyx.common.annotation.NotNeedInterceptor;
import cn.cdyx.common.cache.SecuritCache;
import cn.cdyx.common.exception.SystemException;
import cn.cdyx.common.mapper.TBYXEMRInterfaceMapper;
import cn.cdyx.common.pojo.Code;
import cn.cdyx.common.pojo.TBYXEMRInterface;
import cn.cdyx.common.utils.SecuritUtil;
import cn.cdyx.common.utils.TableNameUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @Author Watermelon
 * @Date 2023-01-17 13:13
 * 移动护理接口验证拦截器
 */
@Component
@Slf4j
public class YDHLInterceptor implements HandlerInterceptor {
    @Autowired
    private TBYXEMRInterfaceMapper tbyxemrInterfaceMapper;
    @Value("${ydhl.key}")
    private String Key_;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)){
            return true;
        }
        byte[] bodyBytes = StreamUtils.copyToByteArray(request.getInputStream());
        String body = new String(bodyBytes, request.getCharacterEncoding());
        log.info("请求参数："+body);
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        NotNeedInterceptor annotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), NotNeedInterceptor.class);
        if(annotation!=null){
            return true;
        }else {
            if (SecuritCache.securitMap.containsKey("YDHL")){
                return true;
            }
            /*数据库读取加密方式
            TBYXEMRInterface ydhl = tbyxemrInterfaceMapper.selectById("YDHL");*/
            if (Key_==null && Key_==""){
                throw new SystemException(Code.INTERFACE_NOTREGISTER,"接口未注册",null);
            }
            String tbyldwjbxx = TableNameUtil.getTableName("TBYLDWJBXX", "");

            List<String> hospitalNameList = tbyxemrInterfaceMapper.selectHospitalName(tbyldwjbxx);
            String hospitalName = hospitalNameList.get(0);
            String decrypt = SecuritUtil.decrypt(Key_, Code.YDHL_INTERFACE_KEY);
            if(Objects.equals(hospitalName,decrypt)){
                SecuritCache.securitMap.put("YDHL",hospitalName);
                return true;
            }else{
                throw new SystemException(Code.INTERFACE_NOTREGISTER_CODE,"接口注册码错误！",null);
            }

        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

